package com.example.webtest;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Resiger extends AppCompatActivity {
    private static final String URL = "https://wanandroid.com/user/registerV2";
    private static final String CODE = "2020";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resiger);
        Button button = findViewById(R.id.button);
        Button button1 = findViewById(R.id.button2);
        button.setOnClickListener(new buttonOnclick());
        button1.setOnClickListener(new returnPage());
    }
    class buttonOnclick implements View.OnClickListener{
        int result = 1;
        @Override
        public void onClick(View v) {
            String username = ((EditText)findViewById(R.id.rusername)).getText().toString();
            String password = ((EditText)findViewById(R.id.rpassward)).getText().toString();
            String rpassword = ((EditText)findViewById(R.id.rrpassword)).getText().toString();
            if (rpassword.equals(password)){
                new Thread(){
                    @Override
                    public void run() {
                        String username = ((EditText)findViewById(R.id.rusername)).getText().toString();
                        String password = ((EditText)findViewById(R.id.rpassward)).getText().toString();
                        result = resiger(URL,username,password);
                    }
                }.start();
            }
            else{
                Toast.makeText(Resiger.this, "两次密码不一样！", Toast.LENGTH_SHORT).show();
            }
            if (result == 1){
                Toast.makeText(Resiger.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(Resiger.this, "注册成功！请重新登陆！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Resiger.this,MainActivity.class);
                startActivity(intent);
            }
        }
    }
    class returnPage implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Resiger.this,MainActivity.class);
            startActivity(intent);
        }
    }
    public int resiger(String url, String username, String password){
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        try {
            HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            String data = "username=" + URLEncoder.encode(username,"UTF-8") + "&password=" + URLEncoder.encode(password,"UTF-8") + "&repassword=" + password + "&verifyCode=" + CODE;
            OutputStream outputStream = urlConnection.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();
            if (urlConnection.getResponseCode() == 200){
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();
                String line;
                while((line = bufferedReader.readLine()) != null){
                    stringBuffer.append(line);
                }
                System.out.println(stringBuffer.toString());
                String string = stringBuffer.toString();
                JSONObject jsonObject = new JSONObject(string);
                String errorMsg = jsonObject.optString("errorCode");
                if (errorMsg.equals("0")){
                    Uri uri = Uri.parse("https://wanandroid.com/index");
                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(intent);
                    return 0;
                }
                return 1;
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
